package com.jxb.lession78.web.servlet;

import com.github.pagehelper.PageInfo;
import com.jxb.lession78.common.Constant;
import com.jxb.lession78.entity.Emp;
import com.jxb.lession78.services.EmpServices;
import com.jxb.lession78.services.impl.EmpServicesImpl;
import com.jxb.lession78.util.DateUtil;
import com.jxb.lession78.util.StringUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/emp")
public class EmpController extends BaseController {
    private EmpServices es = new EmpServicesImpl();

    public String updateEmp(HttpServletRequest req, HttpServletResponse resp){
        int eno = StringUtil.StringToInt(req.getParameter("eno"));
        String ename = req.getParameter("ename");
        String pwd = req.getParameter("pwd");
        String esex = req.getParameter("esex");
        int dno = StringUtil.StringToInt(req.getParameter("dno"));
        int salary = StringUtil.StringToInt(req.getParameter("salary"));
        Date bornDate = DateUtil.stringToDate(req.getParameter("born_date"));
        boolean result = es.updateEmp(new Emp(eno,ename,esex,salary,pwd,dno,bornDate));
        String msg = "修改失败";
        if (result) {
            msg = "修改成功";
            req.setAttribute("msg",msg);
            return Constant.FORWARD + "/message.jsp";
        }
        req.setAttribute("msg",msg);
        return Constant.FORWARD + "/message.jsp";
    }

    public String deleteEmp(HttpServletRequest req, HttpServletResponse resp){
        int eno = StringUtil.StringToInt(req.getParameter("eno"));
        boolean result = es.deleteEmp(eno);
        if (result) {
            return "1";
        }
        return "0";
    }

    public String getEmpByEno(HttpServletRequest req, HttpServletResponse resp){
        int eno = StringUtil.StringToInt(req.getParameter("eno"));
        Emp emp = es.getEmpByEno(eno);
        if (emp != null) {
            req.setAttribute("emp",emp);
            return Constant.FORWARD + "/updateEmp.jsp";
        }
        return Constant.REDIRECT + "/emp?method=getAll";
    }

    public String addEmp(HttpServletRequest req, HttpServletResponse resp){
        String ename = req.getParameter("ename");
        String pwd = req.getParameter("pwd");
        String esex = req.getParameter("esex");
        int dno = StringUtil.StringToInt(req.getParameter("dno"));
        int salary = StringUtil.StringToInt(req.getParameter("salary"));
        Date bornDate = DateUtil.stringToDate(req.getParameter("born_date"));
        boolean result = es.addEmp(new Emp(ename,esex,salary,pwd,dno,bornDate));
        if (result) {
            req.setAttribute("msg","新增成功");
            return Constant.FORWARD + "/message.jsp";
        }
        req.setAttribute("msg","新增失败");
        return Constant.FORWARD + "/message.jsp";
    }

    public String login(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Emp emp = es.getEmpByEmpname(username);
        if (emp != null && emp.getPwd().equals(password)) {
            req.getSession().setAttribute("emp", emp);
            return Constant.REDIRECT + "/emp?method=getAll";
        }
        return Constant.REDIRECT + "/login.jsp";
    }

    public String getAll(HttpServletRequest req, HttpServletResponse resp) {
        String ename = req.getParameter("ename");
        String start = req.getParameter("start");
        String end = req.getParameter("end");
        String esex = req.getParameter("esex");
        int pageNum = StringUtil.StringToInt(req.getParameter("pageNum"));
        Map<String, String> map = new HashMap<>();
        map.put("ename", ename);
        map.put("start", start);
        map.put("end", end);
        map.put("esex", esex);

        PageInfo<Emp> info = es.getEmpByMap(map, pageNum);
        req.setAttribute("empList",info);
        return Constant.FORWARD + "/index.jsp";
    }
}

